package com.yzy.soft.system.monitor;

import com.sun.management.OperatingSystemMXBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.management.ManagementFactory;


public class Memory extends SuperMonitorInfo {

    private static final Logger logger = LoggerFactory.getLogger(Memory.class);
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    private static final File versionFile = new File("/proc/version");
    private static String linuxVersion = null;
    private ParticularMemoryInfo infoBean = new ParticularMemoryInfo();
    private static final double KB = 1024D;
    private static final double MB = 1048576D;
    private static final double GB = 1073741824D;

    /**
     * 获得当前的监控对象.
     *
     * @return 返回构造好的监控对象
     */
    public ParticularMemoryInfo getMemoryInfo() throws Exception {
//        // 可使用内存
//        double totalMemory = Runtime.getRuntime().totalMemory() / KB;
//        // 剩余内存
//        double freeMemory = Runtime.getRuntime().freeMemory() / KB;
//        // 最大可使用内存
//        double maxMemory = Runtime.getRuntime().maxMemory() / KB;
//
//        // 操作系统
//        String osName = System.getProperty("os.name");
//        // 总的物理内存
//        double totalMemorySize = osmxb.getTotalPhysicalMemorySize() / KB;
//        // 剩余的物理内存
//        double freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / KB;
//        // 已使用的物理内存
//        double usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
//                .getFreePhysicalMemorySize())
//                / KB;



        // 可使用内存
        double totalMemory = Runtime.getRuntime().totalMemory();
        // 剩余内存
        double freeMemory = Runtime.getRuntime().freeMemory();
        // 最大可使用内存
        double maxMemory = Runtime.getRuntime().maxMemory();

        // 操作系统
        String osName = System.getProperty("os.name");
        // 总的物理内存
        double totalMemorySize = osmxb.getTotalPhysicalMemorySize();
        // 剩余的物理内存
        double freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
        // 已使用的物理内存
        double usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
                .getFreePhysicalMemorySize());


        // 构造返回对象
//        MemoryInfo infoBean = new MemoryInfo();
//        infoBean.setFreeMemory(freeMemory);
//        infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
//        infoBean.setMaxMemory(maxMemory);
//        infoBean.setOsName(osName);
//        infoBean.setTotalMemory(totalMemory);
//        infoBean.setTotalMemorySize(totalMemorySize);
//        infoBean.setUsedMemory(usedMemory);


//        infoBean.setFreeMemoryG(changeGb(freeMemory,GB));
//        infoBean.setFreePhysicalMemorySizeG(changeGb(freePhysicalMemorySize,GB));
//        infoBean.setMaxMemoryG(changeGb(maxMemory,GB));
////        infoBean.setOsName(osName);
//        infoBean.setTotalMemoryG(changeGb(totalMemory,GB));
//        infoBean.setTotalMemorySizeG(changeGb(totalMemorySize,GB));
//        infoBean.setUsedMemoryG(changeGb(usedMemory,GB));


        infoBean.setFreeMemoryG(freeMemory / GB);
        infoBean.setFreePhysicalMemorySizeG(freePhysicalMemorySize / GB);
        infoBean.setMaxMemoryG(maxMemory / GB);
        infoBean.setTotalMemoryG(totalMemory / GB);
        infoBean.setTotalMemorySizeG(totalMemorySize / GB);
        infoBean.setUsedMemoryG(usedMemory / GB);

        return infoBean;
    }

    private double changeGb(double memorySize, double unit, int scale) {
//return  Arith.div(memorySize,unit,scale);
        return memorySize / unit;
    }

    private double changeGb(long memorySize, long unit) {
        return this.changeGb(memorySize, unit, 2);
    }


    /**
     * 测试方法.
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Memory c = new Memory();

//        MemoryInfo monitorInfo = c.getMemoryInfo();
//
//        logger.info("可使用内存=" + monitorInfo.getTotalMemory()+" "+monitorInfo.getTotalMemory()/1024+"M");
//        logger.info("剩余内存=" + monitorInfo.getFreeMemory()+" "+monitorInfo.getFreeMemory()/1024+"M");
//        logger.info("最大可使用内存=" + monitorInfo.getMaxMemory()+" "+monitorInfo.getMaxMemory()/1024+"M");
//
//        logger.info("操作系统=" + monitorInfo.getOsName());
//        logger.info("总的物理内存=" + monitorInfo.getTotalMemorySize() + "KB"+" "+monitorInfo.getTotalMemorySize()/1024+"M"+" "+Arith.div(monitorInfo.getTotalMemorySize(), 1024*1024, 2)+"G");
//        logger.info("剩余的物理内存=" + monitorInfo.getFreeMemory() + "KB"+" "+monitorInfo.getFreeMemory()/1024+"M"+" "+Arith.div(monitorInfo.getFreeMemory(), 1024*1024, 2)+"G");
//        logger.info("已使用的物理内存=" + monitorInfo.getUsedMemory() + "KB"+" "+monitorInfo.getUsedMemory()/1024+"M"+" "+Arith.div(monitorInfo.getUsedMemory(), 1024*1024, 2)+"G");


    }


}
